package com.peanutplan.blog.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peanutplan.blog.controller.BaseController;
import com.peanutplan.blog.dao.CollectionVoMapper;
import com.peanutplan.blog.dao.UserVoMapper;
import com.peanutplan.blog.exception.TipException;
import com.peanutplan.blog.model.bo.RestResponseBo;
import com.peanutplan.blog.model.vo.CollectionVo;
import com.peanutplan.blog.model.vo.CollectionVoExample;
import com.peanutplan.blog.model.vo.UserVo;
import com.peanutplan.blog.service.IUserService;

/**
 * Created by 13 on 2017/2/26.
 */
@Controller
@RequestMapping("admin/collection")
public class CollectionController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CollectionController.class);

    @Resource
    private CollectionVoMapper collectionDao;
    
    @Resource
    private IUserService userService;
    
    @Resource
    private UserVoMapper userDao;

    /**
     * 用户列表
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "15") int limit, HttpServletRequest request) {
    	CollectionVoExample collectionVoExample = new CollectionVoExample();
    	collectionVoExample.setOrderByClause("created desc");
    	collectionVoExample.createCriteria().andUidEqualTo(this.user(request).getUid());
    	
    	PageHelper.startPage(page, limit);
        List<CollectionVo> commentVos = collectionDao.selectByExample(collectionVoExample);
        PageInfo<CollectionVo> pageInfo = new PageInfo<>(commentVos);
        request.setAttribute("collections", pageInfo);
        return "admin/collection_list";
    }

    /**
     * 删除一条评论
     * @param coid
     * @return
     */
    @PostMapping(value = "delete")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo delete(@RequestParam Integer cid, HttpServletRequest request) {
        try {
        	CollectionVoExample collectionVoExample = new CollectionVoExample();
            UserVo user = this.user(request);
            collectionVoExample.createCriteria().andUidEqualTo(user.getUid()).andCidEqualTo(cid);
            
            collectionDao.deleteByExample(collectionVoExample);
        } catch (Exception e) {
            String msg = "收藏删除失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                LOGGER.error(msg, e);
            }
            return RestResponseBo.fail(msg);
        }
        return RestResponseBo.ok();
    }
    
}
